﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Text;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
/// <summary>
/// Share 的摘要说明


/// </summary>
public class Share : Page
{
    public Share()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }


    /// <summary>
    /// 用户
    /// </summary>
    ///USERS_CODE      0     用户编号
    ///USERS_NAME      1     用户中文名


    ///USERS_UGCODE    2     用户所在角色组编号
    public static string UserValue(int p)
    {

        if (HttpContext.Current.Session["Users"] != null)
        {
            string[] users = (string[])HttpContext.Current.Session["Users"];
            return users[p];
        }
        else
        {
            return "";
        }
    }


    #region 后台弹窗
    public static void Alert(Page page,string p)
    {
        page.RegisterStartupScript("StartUp", "<script language=javascript>alert('" + p + "')</script>");
    }
    #endregion
    #region 窗口关闭
    public static void WinClose(Page page)
    {
        page.RegisterStartupScript("WinClose", "<script language=javascript>window.close();</script>");
    }
    #endregion

    #region 系统操作记录
    public static void InsertLog(string func,string opecode)
    {
        Model.SYSLOG sl = new Model.SYSLOG();
        sl.SL_USERCODE = UserValue(0);
        sl.SL_FUNCCODE = func;
        sl.SL_OPCODE = opecode;
        sl.SL_TIME = DateTime.Now;
        new BLL.SYSLOG().Add(sl);
    }
    #endregion



    #region 生成缩略图


    public static void Convert(System.Drawing.Image image,string dest, int width, int height)
    {
        FileInfo fi = new FileInfo(dest);
        
        if (!fi.Directory.Exists)
            fi.Directory.Create();

        double scale = width * image.Height < image.Width * height ? width * 1.0 / image.Width : height * 1.0 / image.Height;

        int realwidth = (int)Math.Floor(image.Width * scale + 0.5);
        int realheight = (int)Math.Floor(image.Height * scale + 0.5);

        Bitmap bitmap = new Bitmap(image, new Size(realwidth, realheight));

        Bitmap result = new Bitmap(width, height);
        Graphics g = Graphics.FromImage(result);
        g.FillRectangle(Brushes.White, 0, 0, width, height);
        //设置高质量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

        //设置高质量,低速度呈现平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        //清空画布并以透明背景色填充


        g.Clear(Color.White);
        g.DrawImage(bitmap, ((int)(width - realwidth) / 2), (int)((height - realheight) / 2));

        File.Delete(dest);
        result.Save(dest, ImageFormat.Jpeg);
    }
    #endregion

    #region 返回是否启用动态属性

    public static string GetIsDynamic()
    {
        return ConfigurationManager.AppSettings["DynamicProperty"].ToString();
    }

    #endregion

    #region 是否启用促销方案
    public static string IsExistPromoteSales
    {
        get
        {
            return ConfigurationManager.AppSettings["IsExistPromoteSales"].ToString();
        }
    }
    #endregion

    #region 全角转半角
    /// <summary>
    /// 转半角的函数(DBC case)
    /// </summary>
    /// <param name="input">任意字符串</param>
    /// <returns>半角字符串</returns>
    ///<remarks>
    ///全角空格为12288，半角空格为32
    ///其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
    ///</remarks>
    public static string ToDBC(string input)
    {
        char[] c = input.ToCharArray();
        for (int i = 0; i < c.Length; i++)
        {
            if (c[i] == 12288)
            {
                c[i] = (char)32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char)(c[i] - 65248);
        }
        return new string(c);
    }
    #endregion
}
